<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/5
 * Time: 10:40
 * 淘宝客接口
 */

namespace app\api\controller;

use app\common\controller\Api;
use think\Request;

class Tbk extends Api {
	protected $noNeedRight = ['*'];
	protected $noNeedLogin = ['*'];
	protected $topClient = null;
	protected $tbkAppkey = '25310534';
	protected $tbkSecret = 'd64216bf42d2a1ca5c8c97bac50a028e';
	
	public function __construct(Request $request = null)
	{
		parent::__construct($request);
		vendor('tbksdk.TopSdk');
		$this->topClient = new \TopClient();
		$this->topClient->appkey = $this->tbkAppkey;
		$this->topClient->secretKey = $this->tbkSecret;
	}
	
	
	public function searchGoods(){
		$keyword = input('post.keyword');
		$c = $this->topClient;
		$req = new \TbkItemGetRequest();
		$req->setFields("num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url,seller_id,volume,nick");
		$req->setQ($keyword);
//		$req->setCat("16,18");
//		$req->setItemloc("杭州");
//		$req->setSort("tk_rate_des");
//		$req->setIsTmall("false");
//		$req->setIsOverseas("false");
//		$req->setStartPrice("10");
//		$req->setEndPrice("10");
//		$req->setStartTkRate("123");
//		$req->setEndTkRate("123");
//		$req->setPlatform("1");
//		$req->setPageNo("123");
//		$req->setPageSize("20");
		$resp = $c->execute($req);
		$this->success('查询成功', $resp);
	}
	
	/**
	 *  淘宝客淘口令
	 */
	public function tpwdCreate(){
		$url = input('post.goods_url');
		$goodsName = input('post.goods_name');
		$c = $this->topClient;
		$req = new \TbkTpwdCreateRequest;
//		$req->setUserId("123");
		$req->setText($goodsName);
		$req->setUrl($url);
//		$req->setLogo("https://uland.taobao.com/");
//		$req->setExt("{}");
		$resp = $c->execute($req);
		$this->success('生成成功', $resp);
	}
	
	
	/**
	 *  好券清单API【导购】
	 */
	public function dgItemCouponGet(){
		$page = input('post.page', 1, 'intval');
		$c = $this->topClient;
		$req = new \TbkDgItemCouponGetRequest;
		$req->setAdzoneId("65841850062");
		$keyword = input('post.keyword');
		
		
//		$req->setPlatform("2");
//		$req->setCat("16,18");
		$req->setPageSize("20");
		if($keyword && !empty($keyword)) {
			$req->setQ($keyword);
			$req->setPageNo($page);
			$resp = $c->execute($req);
		} else {
			$req->setPageNo($page);
			$resp = $c->execute($req);
		}
		
		$this->success('查询成功', $resp);
	}
	
	/**
	 *  通用物料搜索API（导购）
	 */
	public function dgMaterialOptional () {
		$c = $this->topClient;
		$req = new \TbkDgMaterialOptionalRequest;
		$req->setAdzoneId("65841850062");
		$keyword = input('post.keyword');
//
//		$req->setStartDsr("10");
//		$req->setPageSize("20");
//		$req->setPageNo("1");
//		$req->setPlatform("1");
//		$req->setEndTkRate("1234");
//		$req->setStartTkRate("1234");
//		$req->setEndPrice("10");
//		$req->setStartPrice("10");
//		$req->setIsOverseas("false");
//		$req->setIsTmall("false");
//		$req->setSort("tk_rate_des");
//		$req->setItemloc("杭州");
//		$req->setCat("16,18");
		$req->setQ($keyword);
//		$req->setMaterialId("2836");
//		$req->setHasCoupon("false");
//		$req->setIp("13.2.33.4");
//		$req->setNeedFreeShipment("true");
//		$req->setNeedPrepay("true");
//		$req->setIncludePayRate30("true");
//		$req->setIncludeGoodRate("true");
//		$req->setIncludeRfdRate("true");
//		$req->setNpxLevel("2");
//		$req->setEndKaTkRate("1234");
//		$req->setStartKaTkRate("1234");
//		$req->setDeviceEncrypt("MD5");
//		$req->setDeviceValue("xxx");
//		$req->setDeviceType("IMEI");
		$resp = $c->execute($req);
		$this->success('', $resp);
	}
	
	/**
	 * 阿里妈妈推广券信息查询
	 */
	public function couponGet(){
		$goodsId = input('post.goods_id');
		$c = $this->topClient;
		$req = new \TbkCouponGetRequest;
		$req->setMe("");
		$req->setItemId($goodsId);
		$req->setActivityId("");
		$resp = $c->execute($req);
		$this->success('查询成功', $resp);
	}
	
	
	/**
	 * 商品详情
	 */
	public function itemInfoGet(){
		$c = $this->topClient;
		$req = new \TbkItemInfoGetRequest;
		$req->setNumIids("123,456");
		$req->setPlatform("2");
//		$req->setIp("11.22.33.43");
		$resp = $c->execute($req);
	}
	

}